package siam.ui;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import siam.core.MasterOfGame;

public class InactivePiecesPanel extends JPanel implements ICustomJComponent{

	private List<Piece> listPieces = new ArrayList<Piece>();
	private final int NUMBER_PIECES = 5;
	private Joueur joueur;
	private boolean activate;

	public InactivePiecesPanel(Joueur joueur) {
		this.joueur = joueur;
		for (int i = 0; i < NUMBER_PIECES; i++) {
			Piece p = new Animal(joueur);
			this.listPieces.add(p);
		}

		this.addMouseListener(new MouseListener() {

			public void mouseReleased(MouseEvent arg0) {
			}

			public void mousePressed(MouseEvent arg0) {
			}

			public void mouseExited(MouseEvent arg0) {
			}

			public void mouseEntered(MouseEvent arg0) {
			}

			public void mouseClicked(MouseEvent arg0) {
				if(MasterOfGame.instance.getJoueurTurn()!=null&&MasterOfGame.instance.getJoueurTurn().equals(InactivePiecesPanel.this.joueur) && listPieces.size()!=0){
					update();
				}
			}

			
		});
	}private void update() {
		EventControler.instance.notifyState(this);
				
			}

	


	public List<Piece> getListPieces() {
		return listPieces;
	}

	public void addPiece(Piece p) {
		listPieces.add(p);
	}

	public Piece getPiece() {
		if (listPieces.size() == 0) {
			return null;
		}
		Piece p = listPieces.get(0);
		listPieces.remove(0);
		return p;

	}
	
	public Joueur getJoueur() {
		return joueur;
	}

	@Override
	public void paint(Graphics g) {

		super.paint(g);
		if (listPieces.size() != 0) {
			int left = 0, pos = 0, bottom = this.getHeight();
			pos -= bottom / NUMBER_PIECES;
			for (Piece p : listPieces) {
				pos += bottom / NUMBER_PIECES;
				g.drawImage(p.getImage(), left, pos, this.getWidth(),
						this.getHeight() / NUMBER_PIECES, this);
			}
		}else
		{
			this.activate=false;
			EventControler.instance.notifyState(this);
			setBackground(Color.DARK_GRAY);
		}
	}
	public void desactivate() {
		activate = false;
		this.setBackground(Color.DARK_GRAY);
		repaint();
	}
	
	synchronized public void activate() {
		activate = true;
		if (listPieces.size() != 0) {
			this.setBackground(Color.YELLOW);
		}
		
		repaint();
	}
}
